package com.juzhi.dvrapp.utils;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;

import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;



public class Convert {

    private static Gson create() {
        return Convert.GsonHolder.gson;
    }

    private static class GsonHolder {
        private static Gson gson = new Gson();
    }

    public static <T> T fromJson(String json, Class<T> type) throws JsonIOException, JsonSyntaxException {
        return create().fromJson(json, type);
    }

    public static <T> T fromJson(String json, Type type) {
        return create().fromJson(json, type);
    }

    public static <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
        return create().fromJson(reader, typeOfT);
    }

    public static <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
        return create().fromJson(json, classOfT);
    }

    public static <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
        return create().fromJson(json, typeOfT);
    }

    public static String toJson(Object src)throws JsonIOException, JsonSyntaxException{
        return create().toJson(src);
    }

    public static String toJson(Object src, Type typeOfSrc)throws JsonIOException, JsonSyntaxException{
        return create().toJson(src, typeOfSrc);
    }

/*    public static String formatJson(String json) throws JsonIOException, JsonSyntaxException{
        try {
            JsonParser jp = new JsonParser();
            JsonElement je = jp.parse(json);
            return JsonConvertor.getInstance().toJson(je);
        } catch (Exception e) {
            return json;
        }
    }

    public static String formatJson(Object src) throws JsonIOException, JsonSyntaxException{
        try {
            JsonParser jp = new JsonParser();
            JsonElement je = jp.parse(toJson(src));
            return JsonConvertor.getInstance().toJson(je);
        } catch (Exception e) {
            return e.getMessage();
        }
    }*/

    /**
     * 转成json
     *
     * @param object
     * @return
     */
    public static String GsonString(Object object) throws JsonIOException, JsonSyntaxException{
        return create().toJson(object);
    }

    /**
     * 转成bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T GsonToBean(String gsonString, Class<T> cls) throws JsonIOException, JsonSyntaxException{
        return create().fromJson(gsonString, cls);
    }

    /**
     * 转成list
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> List<T> GsonToList(String gsonString, Class<T> cls) throws JsonIOException, JsonSyntaxException{
        return  create().fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
    }

    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) throws JsonIOException, JsonSyntaxException{
        return create().fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
    }

    /**
     * 转成map的
     *
     * @param gsonString
     * @return
     */
    public static <T> Map<String, T> GsonToMaps(String gsonString) throws JsonIOException, JsonSyntaxException{
        return create().fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
    }
}
